package com.btkjgc.controller;

import com.btkjgc.models.LeftMenu;
import com.btkjgc.models.extend.AdminUserModel;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.LastModified;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.List;

/**
 * Created by _ESE on 2015/2/2.
 */
public class BaseController implements LastModified {
	
	@Autowired
	private  HttpSession session;
	
    private long lastModified;  
	
    public	Logger logger = Logger.getLogger(BaseController.class);

	public LeftMenu getMenu(List<LeftMenu> sidebarlist, String uri) {
		if (sidebarlist != null) {
			for (LeftMenu l1 : sidebarlist) {
				if (l1.getUrl() != null && l1.getUrl().equals(uri)) {
					return l1;
				}
				for (LeftMenu l2 : l1.getSubmenus()) {
					if (l2.getUrl() != null && l2.getUrl().equals(uri)) {
						return l2;
					}
					for (LeftMenu l3 : l2.getSubmenus()) {
						if (l3.getUrl() != null && l3.getUrl().equals(uri)) {
							return l3;
						}
					}
				}
			}
		}
		return null;
	}

	// 获取左边的菜单
	@SuppressWarnings("unchecked")
	public ModelAndView LeftBar(ModelAndView mv, HttpServletRequest request) {
		if (mv == null) {
			return null;
		}
		List<LeftMenu> sidebarlist = (List<LeftMenu>) request.getSession()
				.getAttribute("left_menu");

		if (sidebarlist != null && sidebarlist.size() != 0) {

			String uri = request.getServletPath();
			LeftMenu current = getMenu(sidebarlist, uri);
			if (current != null && current.getUrl() != null) {
				uri = current.getNav_url();
			}

			boolean active = false;
			for (LeftMenu l1 : sidebarlist) {
				l1.setActive(false);
				if (l1.getNav_url() != null && l1.getNav_url().equals(uri)) {
					active = true;
					l1.setActive(true);
				}
				for (LeftMenu l2 : l1.getSubmenus()) {
					l2.setActive(false);
					if (l2.getNav_url() != null && l2.getNav_url().equals(uri)) {
						active = true;
						l1.setActive(true);
						l2.setActive(true);
					}
					for (LeftMenu l3 : l2.getSubmenus()) {
						l3.setActive(false);
						if (l3.getNav_url() != null
								&& l3.getNav_url().equals(uri)) {
							active = true;
							l1.setActive(true);
							l2.setActive(true);
							l3.setActive(true);
						}
					}
				}
			}
			if (!active) {
				sidebarlist.get(0).setActive(true);
			}
			mv.addObject("sidebarlist", sidebarlist);
		}

		return mv;
	}

	@Override
	public long getLastModified(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		if (lastModified == 0L) {
			// TODO 此处更新的条件：如果内容有更新，应该重新返回内容最新修改的时间戳
			lastModified = System.currentTimeMillis();
		}
		return lastModified;
	}
	
	
	public   AdminUserModel getLoginUser() {
		 
		AdminUserModel adminUserModel = (AdminUserModel) session.getAttribute("user");
		
		if(adminUserModel==null){
			adminUserModel = new AdminUserModel();
		}
		
		return adminUserModel;
	}
}
